﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media.Imaging;
using Caliburn.Micro;
using JY.Common.Enums;
using JY.Common.Helpers;
using JY.Wpf.Core.Enums;
using JY.Wpf.Core.Events;
using JY.Wpf.Core.Models;
using JY.Wpf.Core.Tools;
using JY.Wpf.Core.ViewModelBases;
using JY.Wpf.TestClient.Commons;
using MahApps.Metro.Controls;
using Extensions = JY.Wpf.Core.Tools.Extensions;

namespace JY.Wpf.TestClient.ViewModels
{
    public class ShellViewModel : MainViewModelBase
    {
        #region # 变量
        #endregion

        #region # 构造器
        /// <summary>
        /// 构造器
        /// </summary>
        public ShellViewModel()
        {
            this.Title = "立高智造";
            Wpf.Controls.Controls.WarningMessageBar.Initialize(this.DeactivateMessage);
            this.LeftMenuViewModel = new LeftMenuViewModel();
        }
        #endregion

        #region # 属性

        #region 当前用户——User CurUser

        /// <summary>
        /// 当前用户
        /// </summary>
        public User CurUser { get; set; }
        #endregion

        #region 系统菜单——LeftMenuViewModel LeftMenuViewModel
        /// <summary>
        /// 系统菜单
        /// </summary>
        public LeftMenuViewModel LeftMenuViewModel { get; set; }
        #endregion

        #region 是否全屏——bool IsFullScreen
        /// <summary>
        /// 是否全屏
        /// </summary>
        public bool IsFullScreen { get; set; }
        #endregion

        #region 照片——BitmapSource BitmapSource

        private BitmapSource _bitmapSource;
        /// <summary>
        /// 照片
        /// </summary>
        public BitmapSource BitmapSource
        {
            get { return this._bitmapSource;}
            set { this._bitmapSource = value;base.NotifyOfPropertyChange(); }
        }
        #endregion
        #region 照片路径——string PhotoPath

        private string _photoPath;
        /// <summary>
        /// 照片路径
        /// </summary>
        public string PhotoPath
        {
            get { return this._photoPath; }
            set { this._photoPath = value; base.NotifyOfPropertyChange(); }
        }
        #endregion
        #endregion

        #region # Command方法

        #region # 全屏——void FullScreen()
        /// <summary>
        /// 全屏
        /// </summary>
        public void FullScreen()
        {
            this.IsFullScreen = true;
            this.SetFullScreen();
        }
        #endregion

        #region # 取消全屏——void CancleFullScreen()
        /// <summary>
        /// 取消全屏
        /// </summary>
        public void CancleFullScreen()
        {
            this.IsFullScreen = false;
            this.SetFullScreen();
        }
        #endregion
        #endregion

        #region # 方法
        #region # 初始化页面——void OnInit()
        /// <summary>
        /// 初始化页面
        /// </summary>
        public override void OnInit()
        {
            this.PhotoPath = @"D:\1.bmp";
            this.BitmapSource = new BitmapImage(new Uri("http://localhost:2026/Photos/1.bmp"));
        }
        #endregion

        #region # 繁忙状态操作——void BusyOperation()
        /// <summary>
        /// 繁忙状态操作
        /// </summary>
        public override void BusyOperation()
        {
            Wpf.Controls.Controls.BusyIndicator.BusyOperation(this.IsBusy, this.BusyContent);
        }

        #endregion

        #region # 完成警告消息回调方法——void DeactivateMessage()
        /// <summary>
        /// 完成警告消息回调方法
        /// </summary>
        private void DeactivateMessage(MessageType messageType)
        {
            //MessageBox.Info($"测试完成警告消息[{messageType}]回调方法。");
            this.EventAggregator.PublishOnUIThreadAsync(new MessageEvent($"测试完成警告消息[{messageType}]回调方法。",MessageType.Info));
        }
        #endregion

        #region # 设置全屏——void SetFullScreen()
        /// <summary>
        /// 设置全屏
        /// </summary>
        private void SetFullScreen()
        {
            var window = Application.Current.MainWindow as MetroWindow;
            if (window == null) return;
            if (this.IsFullScreen)
            {
                window.SetCurrentValue(MetroWindow.IgnoreTaskbarOnMaximizeProperty, true);
                window.SetCurrentValue(Window.WindowStateProperty, WindowState.Maximized);
                window.SetCurrentValue(MetroWindow.UseNoneWindowStyleProperty, true);
            }
            else
            {
                window.SetCurrentValue(Window.WindowStateProperty, WindowState.Normal);
                window.SetCurrentValue(MetroWindow.UseNoneWindowStyleProperty, false);
                window.SetCurrentValue(MetroWindow.ShowTitleBarProperty, true); // <-- this must be set to true
                window.SetCurrentValue(MetroWindow.IgnoreTaskbarOnMaximizeProperty, false);
            }
        } 
        #endregion
        #endregion

        #region # Events方法

        #region # 显示子窗体事件——void Handle(SetChildWindowEvent childWindowEvent)
        /// <summary>
        /// 显示子窗体事件
        /// </summary>
        public override Task HandleAsync(ShowChildWindowEvent eventData, CancellationToken cancellationToken)
        {
            var childWindowViewModel = eventData.ChildWindowViewModel as ChildWindowViewModelBase;
            if (childWindowViewModel == null) return Task.FromResult(false);

            Wpf.Controls.Controls.ChildWindow.Show(childWindowViewModel, eventData.ShowMask);
            return Task.CompletedTask;
        }
        #endregion

        public override Task HandleAsync(WarningMessageEvent eventData, CancellationToken cancellationToken)
        {
            Task.WaitAll(new Task[1]
            {
                Task.Run(() => { while (true)
                {

                }})
            }, cancellationToken);
            
            return base.HandleAsync(eventData, cancellationToken);
        }

        #region # 显示消息事件方法——void Handle(MessageEvent eventData)
        /// <summary>
        /// 显示消息事件方法
        /// </summary>
        public override Task HandleAsync(MessageEvent eventData, CancellationToken cancellationToken)
        {
            switch (eventData.MessageType)
            {
                //case MessageType.Info:
                //    Growl.Info(eventData.Message);
                //    break;
                //case MessageType.Success:
                //    Growl.Success(eventData.Message);
                //    break;
                //case MessageType.Warning:
                //    Growl.Warning(eventData.Message);
                //    break;
                //case MessageType.Error:
                //    Growl.Error(eventData.Message);
                //    break;
                //case MessageType.Ask:
                //    Growl.Ask(eventData.Message, (f1) =>
                //    {
                //        return true;
                //    });
                //    break;
                //case MessageType.Fatal:
                //    Growl.Fatal(eventData.Message);
                //    break;
            }
            return Task.CompletedTask;
        }
        #endregion
        #endregion
    }
}
